package com.wut.clock.controller;

import com.wut.clock.common.api.CommonPage;
import com.wut.clock.common.api.CommonResult;
import com.wut.clock.common.util.SensitiveFilterUtil;
import com.wut.clock.dto.portal.ArticlePortalParams;
import com.wut.clock.factory.number.Number;
import com.wut.clock.factory.number.NumberFactory;
import com.wut.clock.service.IArticleService;
import com.wut.clock.vo.portal.ArticleCountVo;
import com.wut.clock.vo.portal.ArticlePortalVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZYH
 * @unit D9Lab
 * @description 文章管理
 * @date 2022/6/8 10:34
 */
@RequiredArgsConstructor
@RequestMapping("/article")
@RestController
@Api(value="文章管理器",tags = {"文章管理器"})
public class ArticleController {

    private final IArticleService articleService;

    private final SensitiveFilterUtil sensitiveFilterUtil;

    private final NumberFactory numberFactory;

    @ApiOperation("发布一遍文章")
    @PostMapping("/addArticle")
    public CommonResult<String> addArticle(@RequestBody ArticlePortalParams articlePortalParams){
        if(articleService.addArticle(articlePortalParams)>0) return CommonResult.success();
        return CommonResult.failed();
    }


    @ApiOperation("修改一遍文章")
    @PostMapping("/updateArticle")
    public CommonResult<String> updateArticle(@RequestBody ArticlePortalParams articlePortalParams){
        if(articleService.updateArticle(articlePortalParams)>0) return CommonResult.success();
        return CommonResult.failed();
    }


    @ApiOperation("删除一遍文章")
    @PostMapping("/deleteArticle/{articleId}")
    public CommonResult<String> deleteArticle(@PathVariable Integer articleId){
        if(articleService.deleteArticle(articleId)>0) return CommonResult.success();
        return CommonResult.failed();
    }

    @ApiOperation("查询个人文章列表")
    @GetMapping("/getOwnArticleList")
    public CommonResult<CommonPage<ArticlePortalVo>> getOwnArticleList(@RequestParam(required = false,defaultValue = "1")Integer pageNum,
                                                                       @RequestParam(required = false,defaultValue = "10")Integer pageSize,
                                                                       @RequestParam(required = false) Integer categoryId,
                                                                       @RequestParam(required = false) String yearMonth,
                                                                       @RequestParam(required = false) String keywords){
        return CommonResult.success(CommonPage.restPage(articleService.getOwnArticleList(pageNum,pageSize,categoryId,yearMonth,keywords)));
    }


    @ApiOperation("查看文章详情")
    @GetMapping("/getArticleDetail")
    public CommonResult getArticleDetail(@RequestParam Integer articleId){
        return CommonResult.success(articleService.getArticleDetail(articleId));
    }

    @ApiOperation("查询文章下的评论")
    @GetMapping("getArticleComment")
    public CommonResult getArticleComment(@RequestParam(defaultValue = "1") Integer pageNum,
                                          @RequestParam(defaultValue = "10") Integer pageSize,
                                          @RequestParam Integer articleId){
        return CommonResult.success(articleService.getArticleComment(pageNum,pageSize,articleId));
    }


    @ApiOperation("判断文本是否包含敏感词")
    @PostMapping("filter")
    public CommonResult filterText(@RequestParam String text){
        return CommonResult.success(sensitiveFilterUtil.filter(text));
    }

    @ApiOperation("获取文章的浏览量、评论数、收藏数、点赞数和点踩数")
    @GetMapping("getArticleCount")
    public CommonResult getArticleCount(Integer articleId){
        Number article = numberFactory.getNumber("article");
        ArticleCountVo articleCountVo = article.countArticleNumber(articleId);
        return CommonResult.success(articleCountVo);
    }
}
